<script lang="ts" setup>
import { ref } from 'vue';
import type { FormInstance } from '@arco-design/web-vue/es/form';
import { $fetch } from '@/settings/utils/fetch';
import useNotification from '@/settings/hooks/notification';
import { useRouter } from 'vue-router';
import MarkdownEditor from '@/settings/components/markdown-editor/index.vue';
import { useI18n } from 'vue-i18n';

const props = defineProps({
  id: {
    type: String,
    default: '',
  },
});

const loading = ref(false);
const formData = ref({
  emailTemplateName: '',
  emailTemplateTitle: '',
  emailTemplateContent: '',
});
const formRef = ref<FormInstance>();
const router = useRouter();
const { success } = useNotification();
const { t } = useI18n();

const submit = async () => {
  const res = await formRef.value?.validate();

  if (res) {
    return;
  }

  const { isSuccess } = await $fetch(props.id ? '/admin/email/temp/editEmailTemp' : '/admin/email/temp/addEmailTemp', {
    method: 'post',
    data: {
      id: props.id,
      ...formData.value,
    },
    loadingStatus: loading,
  });

  if (isSuccess) {
    success(t('settings.notification.success'));
    router.push('/operator/subscription/template/index');
  }
};

const getDetail = async () => {
  const { isSuccess, data } = await $fetch('/admin/email/temp/getEmailById', {
    data: {
      emailId: props.id,
    },
    loadingStatus: loading,
  });

  if (isSuccess) {
    formData.value = {
      emailTemplateName: data.emailTemplateName,
      emailTemplateTitle: data.emailTemplateTitle,
      emailTemplateContent: data.emailTemplateContent,
    };
  }
}

const init = () => {
  if (props.id) {
    getDetail();
  }
}

init();
</script>

<template>
  <OpenPageSection>
    <OpenGeneralCard>
      <AForm ref="formRef" auto-label-width scroll-to-first-error :model="formData" @submit="submit">
        <AFormItem :label="$t('settings.view.subscription.template.index.column1')" field="emailTemplateName" :rules="[{ required: true, message: $t('settings.notification.required') }]">
          <AInput v-model="formData.emailTemplateName" />
        </AFormItem>

        <AFormItem :label="$t('settings.view.subscription.template.index.column3')" field="emailTemplateTitle" :rules="[{ required: true, message: $t('settings.notification.required') }]">
          <AInput v-model="formData.emailTemplateTitle" />
        </AFormItem>

        <AFormItem :label="$t('settings.view.subscription.template.index.column6')" field="emailTemplateContent" :rules="[{ required: true, message: $t('settings.notification.required') }]" :help="$t('settings.view.subscription.template.edit.help')">
          <MarkdownEditor :height="500" v-model="formData.emailTemplateContent" />
        </AFormItem>

        <ADivider />

        <AFormItem>
          <ASpace>
            <OpenRouterButton to="/operator/subscription/template/index">{{ $t('settings.view.subscription.template.edit.button') }}</OpenRouterButton>
            <AButton html-type="submit" type="primary" :loading="loading">{{ $t('settings.action.submit') }}</AButton>
          </ASpace>
        </AFormItem>
      </AForm>
    </OpenGeneralCard>
  </OpenPageSection>
</template>

<style lang="scss" scoped>
</style>
